package com.ftqh.tutorial

/**
 * Created by ftqh on 2017/7/17.
 */

fun ifMax(){
    var a:Int = 11
    var b:Int = 15
    val max = if (a > b) {
        print("Choose a")
        a
    } else {
        print("Choose b")
        b
    }
    print(": "+max);
}
fun whenExpression(x:Int){
    when (x) {
        1 -> print("x == 1")
        2 -> print("x == 2")
        else -> { // Note the block
            println("x is neither 1 nor 2")
        }
    }
    when (x) {
        0, 1 -> println("x == 0 or x == 1")
        else -> println("otherwise")
    }
    when (x) {
        in 1..10 -> println("x is in the range")
        //in validNumbers -> print("x is valid")
        is Int -> println("x is a Long")
        !in 10..20 -> println("x is outside the range")
        else -> println("none of the above")
    }
}
fun forLoops(){
    var arrays:IntArray = intArrayOf(1,3,5,7,9)
    for (i in arrays.indices) {
        print(arrays[i])
    }
    println("------")
    for ((index, value) in arrays.withIndex()) {
        println("the element at $index is $value")
    }
}
fun map(){
    val mapInit = mutableMapOf(1 to 1, 2 to 2)
    val newMap = mapInit.mapKeys { "key_${it.key}" }.mapValues { "value_${it.value}" }
    println(newMap.get("key_1") + "  " + newMap.get("key_2"))
    println(newMap)
//打印结果 {key_1=value_1, key_2=value_2}
    val pair = Pair("ss","sg")
    val map = mapOf(pair)
    println(map)
    val map1=map.mapKeys { entry -> "${entry.value}!"  }
    for((key,value) in map1){
        println("map1:key=$key")
        println("map1:value=$value")
    }
    val map2 =map.mapKeys { (key, value) -> "$value"  }
    for((key,value) in map2){
        println("map2:key=$key")
        println("map2:value=$value")
    }
    val map3=map.mapValues { entry -> "${entry.value}!" }
    for((key,value) in map3){
        println("map3:key=$key")
        println("map3:value=$value")
    }
    val map4=map.mapValues { (key, value) -> "$value" }
    for((key,value) in map4){
        println("map4:key=$key")
        println("map4:value=$value")
    }

}
fun labelforLoops(){
    loop@ for (i in 1..100) {
        if((i % 5) == 0)
            println("$i is in multiples of 5")
        if(i>50)
            break@loop
    }
}

fun main(args:Array<String>){
    ifMax()
    println("------")
    whenExpression(5)
    println("------")
    forLoops()
    println("------")
    map()
    println("------")
    labelforLoops()
}
